<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-3.5.1.js"></script>
<script type="text/javascript">
	$(function(){
		start(1);
	});

	function pageChange(ppp){
		start(ppp);
	}

	function btn(){
		var p=$("#pageText").val();
		jsonObj.p=p;
		start(1);
	}
	
	function box(){
		alert($("#bbbb")[0].checked);
		var che=$("#bbbb")[0].checked;
		$("table tr td input[type='checkbox']").prop("checked",che);
	}
	/* $("#bbbb").click(function (){
		alert($("#bbbb").checked);
		alert(this.checked);
		var che=$("#bo").checked;
		$("table tr td input[type='checkbox']").attr("checked",che);
	}); */
	
	function update(id){
		window.location.href="${pageContext.request.contextPath}/toUpdate.do/"+id;
	}
	
	
	function deletee(id){
		$.ajax({
			type:"post",
			data:{
				["lists[0].id"]:id
			},
			url:"${pageContext.request.contextPath}/delete.dooo",
			success:function(aaa){
				if(aaa>0){
					alert("删除成功");
					start(1);
				}else{
					alert("更新失败");
					window.location.href="${pageContext.request.contextPath}/error/error";
				}
			}
		});
	}

	var jsonObj2={};
	function deleteBatch(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有选择要删除的对象");
			return false;
		}
		$.each(all,function(i,n){
			jsonObj2["lists["+i+"].id"]=n.id;
			jsonObj2["lists["+i+"].name"]=n.name;
		});
		$.ajax({
			type:"post",
			data:jsonObj2,
			url:"${pageContext.request.contextPath}/delete.dooo",
			success:function(aaa){
				if(aaa>0){
					alert("删除"+aaa+"条记录成功");
					start(1);
				}else{
					alert("删除失败");
					window.location.href="${pageContext.request.contextPath}/error/error";
				}
			}
		});
	}
	
	var jsonObj={
		"pn":1,
	};
	function start(oooo){
		jsonObj.pn=oooo;
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/showUser2.do",
			data:jsonObj,
			success:function(result){
				var content="<tr><th><input type='checkbox' id='bbbb' name='a' onclick='box()'></th><th>id</th><th>用户名</th><th>密码</th><th>权限</th></tr>";
				$.each(result.list,function(i,n){
					content+="<tr><td><input type='checkbox' id='"+n.id+"' name='"+n.name+"'></td><td>"+n.id+"</td><td>"+n.username
						+"</td><td>"+n.password+"</td><td>"+n.permissions
						+"</td><td><button type='button' id='update' onclick='update("+n.id+")'>更新</button></td>"
						+"<td><button type='button' id='delete' onclick='deletee("+n.id+")'>删除</td></tr>";
				});
				$("#tab").html(content);
				
				var content2="当前第:"+result.pageNum+"页，总共："+result.pages+"页，总共："+result.total+"条记录";
				$("#fy1").html(content2);
				
				var content3="";
				content3+="<a href='#' onclick='pageChange(1)'>首页</a>";
				if(result.hasPreviousPage){
					content3+="<a href='#' onclick='pageChange("+(result.pageNum-1)+")'><span>&laquo;</span</a>";
				}
				/* //alert(result.pageNum);
				for(var i=1;i<=result.pages;i++){
					if(i==result.pageNum){
						content3+="<a href='#'>"+i+"</a>"
					}else{
						content3+="<a href='#' onclick=pageChange("+i+")>"+i+"</a>";
					}
				}  */
				//alert(result.navigatepageNums);
				$.each(result.navigatepageNums,function(i,n){
					if(n==result.pageNum){
						content3+="<a href='#'>"+n+"</a>"
					}else{
						content3+="<a href='#' onclick=pageChange("+n+")>"+n+"</a>";
					}
				});
				
				if(result.hasNextPage){
					content3+="<a href='#' onclick='pageChange("+(result.pageNum+1)+")'><span>&raquo;</span</a>";
				}
				content3+="<a href='#' onclick='pageChange("+result.pages+")'>末页</a>";
				$("#fy2").html(content3);
			}
		});
	}

	function ins(){
		window.location.href="${pageContext.request.contextPath}/toInsertUser";
	}
</script>
<body>
	<div>
		<input type="text" id="pageText" placeholder="请输入查询条件"><button type="button" onclick="btn()">搜索</button>
		<button type="button" id="insert" onclick="ins()">添加用户</button>
		<button type="button" onclick="deleteBatch()">批量删除</button>	
	</div>
	<div><table id="tab"></table></div>
    <div >
        <div id="fy1"></div>
        <div id="fy2"></div>
        
    </div>
</body>
</html>